package cn.qyd.sort;

import java.util.Arrays;

/**
 * @author 邱运铎
 * @date 2024-03-25 22:32
 */
public class QuickSort {
    public static void main(String[] args) {
//        int[] arr = new int[]{4,2,1,5,6};
//        int[] arr = new int[]{4,2,1};
        int[] arr = new int[]{4,2,1,5,34,6,12,45,13,23,18,34};
        quickSort(arr,0,arr.length - 1);
        System.out.println(Arrays.toString(arr));

    }

    public static void quickSort(int[] arr, int left, int right) {

        int first = left;
        int last = right;
        int num = arr[right];
        System.out.println(first);
        System.out.println(last);
        if (left >= last) {
            return;
        }
        while (first < last) {
            while (first < last && arr[first] <= num) {
                first++;
            }
            while (first < last && arr[last] > num) {
                last--;
            }
            System.out.println(first);
            System.out.println(last);
            if (first < last) {
                arr[first] ^= arr[last];
                arr[last] ^= arr[first];
                arr[first] ^= arr[last];
            }
        }
        Arrays.toString(arr);
        System.out.println(first);
        System.out.println(last);
        quickSort(arr,left,first - 1);
        quickSort(arr,last, right);

    }
}
